package com.thh.filesystem.uitl;



import com.thh.filesystem.exception.ExceptionCast;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传下载工具类
 */

public class FastDFSClient2 {


    //加载配置文件
    static {


        //   String conf_filename =  new ClassPathResource("fdfs_client.conf").getPath();

        //初始化配置文件
        try {
            ClientGlobal.init("fdfs_client.conf");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取文件服务器地址
     *
     * @return 服务器地址
     */
    public static String getTrackerUrl() {

        try {
            //跟踪服务器的服务端
            TrackerServer trackerServer = getTrackerServer();
            //跟踪服务器地址
            String hostAddress = trackerServer.getInetSocketAddress().getAddress().getHostAddress();
            //端口
            int port = ClientGlobal.getG_tracker_http_port();
            String url = "http://" + hostAddress + ":" + port;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取trackerServer 客户端
     *
     * @return
     * @throws IOException
     */
    private static TrackerServer getTrackerServer() throws IOException {

        // 1、创建跟踪服务器客户端
        TrackerClient trackerClient = new TrackerClient();

        // 2、获取跟踪服务器
        return trackerClient.getConnection();

    }


    /**
     * 上传文件
     *
     * @param multipartFile 文件对象
     * @return  String[]  数组0 group  数组1 M00/00/00/wKjFhF6xCRqAZ25hAEnkGVWPIpE921.png
     *                    合并就是路径
     */
    public static String[] uploadFile(MultipartFile multipartFile) {
        if (StringUtils.isEmpty(multipartFile)){
           throw new RuntimeException("非法参数");
        }


       // int beginIndex = multipartFile.getOriginalFilename().lastIndexOf(".");
       // int endIndex = multipartFile.getOriginalFilename().length()-1;
       // String file_ext_name = multipartFile.getOriginalFilename().substring(beginIndex, endIndex);
        String file_ext_name = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename());

         NameValuePair[] meta_list = new NameValuePair[2];//存放文件的其他信息
         meta_list[0] = new NameValuePair(multipartFile.getContentType());
         meta_list[1] = new NameValuePair(multipartFile.getSize()+"");

         try {
             String[] uploadResult = uploadFile(multipartFile.getBytes(), file_ext_name, meta_list);
             return uploadResult;
         }catch (Exception e){
             e.printStackTrace();
         }

         return null;

    }






    /**
     * @author thh
     * @Description 文件上传
     * @Date 17:42 2020/4/11
     * @param file_buff 附件
     * @param ext_name 附件扩展名
     * @param des 附件备注
     * @return java.lang.String[]
     **/
    public static String[] uploadFile(byte[] file_buff, String ext_name, String des){
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair(des);
        String[] uploadResult = uploadFile(file_buff, ext_name, meta_list);
        return uploadResult;
    }


    /**
     * @author thh
     * @Description 文件上传
     * @Date 17:42 2020/4/11
     * @param file_buff 附件
     * @param ext_name 附件扩展名
     * @param meta_list 存放文件的其他信息
     * @return java.lang.String[]
     **/

    public static String[] uploadFile(byte[] file_buff, String ext_name, NameValuePair[] meta_list){

        try {

            // 1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            // 2、获取跟踪服务器
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件上传
            String[] uploadResult = storageClient.upload_file(file_buff, ext_name, meta_list);
            return uploadResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 下载附件
     *
     * @param group_name      组名
     * @param remote_filename 文件名
     * @return
     */
    public static byte[] downFile(String group_name, String remote_filename) {

        try {
            TrackerServer trackerServer = getTrackerServer();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] bytes = storageClient.download_file(group_name, remote_filename);
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static byte[] downFile(String path) {

        try {
            TrackerServer trackerServer = getTrackerServer();
            // 3、创建存储服务器客户端
            StorageClient storageClient1 = new StorageClient1(trackerServer, null);
            byte[] bytes = ((StorageClient1) storageClient1).download_file1(path);
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 下载附件
     * @param group_name 组名
     * @param remote_filename   附件名字
     */
    public static void deleteFile(String group_name, String remote_filename){
        try {
            TrackerServer trackerServer = getTrackerServer();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
